package com.eshop.pojo;


import com.eshop.config.CustomAuthorityDeserializer;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

        import java.io.Serializable;
        import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel("用户实体类")
public class User implements Serializable, UserDetails {
    @ApiModelProperty(value = "用户id")
    private int id;
    @ApiModelProperty(value = "用户密码")
    private String password;
    @ApiModelProperty(value = "用户账号")
    private String username;
    @ApiModelProperty(value = "联系电话")
//        @JsonProperty("uPhoneNum")
    private String uPhoneNum;
    @ApiModelProperty(value = "联系地址")
//        @JsonProperty("uAddress")
    private String uAddress;
    @ApiModelProperty(value = "用户昵称")
//        @JsonProperty("uNickName")
    private String uNickName;
    @ApiModelProperty(value = "禁用状态")
    private int isEnabled;
    @ApiModelProperty(value = "用户头像url")
    private String userFace;
    @ApiModelProperty(value = "用户角色")
    private List<Role> roles;


    @Override
    @JsonDeserialize(using = CustomAuthorityDeserializer.class)
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = roles
                .stream()
                .map(role -> new SimpleGrantedAuthority(role.getName()))
                .collect(Collectors.toList());
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        if (isEnabled==1) return true;
        else return false;
    }
}

